/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.mobiubiq.directory.client.frame;

import org.kalmeo.kuix.core.Kuix;
import org.kalmeo.util.frame.FrameHandler;
import org.mobiubiq.directory.client.object.AbstractObject;
import org.mobiubiq.directory.client.object.BarcodeObject;
import org.mobiubiq.directory.client.widget.BarcodeListener;
import org.mobiubiq.directory.client.widget.BarcodeReaderWidget;

/**
 *
 * @author Marnix Renne
 */
public class BarcodeReader extends ScreenDefinitionBaseFrame implements BarcodeListener {
    public BarcodeReader() {
        super("read-barcode.xml");
    }
    
    public boolean onMessage(Object identifier, Object[] arguments) {
        if(super.onMessage(identifier, arguments)) {
            if("barcode".equals(identifier)) {
                // pop current frame from stack
                FrameHandler handler = Kuix.getFrameHandler();
                AbstractObject object = (AbstractObject) arguments[0];
                handler.pushFrame(new Services(object));
                this.remove();
                return false;
            }
            if("takeSnapshot".equals(identifier)) {
                BarcodeReaderWidget widget = (BarcodeReaderWidget) Kuix.getCanvas().getDesktop().getWidget("reader");
                widget.takeSnapshot();
                return false;
            }
            return true;
        } else {
            return false;
        }
    }

    public void onAdded() {
        super.onAdded();
        BarcodeReaderWidget widget = (BarcodeReaderWidget) Kuix.getCanvas().getDesktop().getWidget("reader");
        widget.addBarcodeListener(this);
    }

    /*
    protected void cleanUp() {
        //super.cleanUp();
        //BarcodeReaderWidget widget = (BarcodeReaderWidget) Kuix.getCanvas().getDesktop().getWidget("reader");
        //widget.cleanUp();
    }*/

    
    public void barcodeDetected(BarcodeObject object) {
        onMessage("barcode", new Object[] {object});
    }
}
